package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Bed;
import com.woniuxy.oldmanapi.entity.Older;
import com.woniuxy.oldmanapi.mapper.BedMapper;
import com.woniuxy.oldmanapi.mapper.OlderMapper;
import com.woniuxy.oldmanapi.vo.NumVo;
import com.woniuxy.oldmanapi.xvo.OlderVo;
import com.woniuxy.oldmanapi.yvo.OlderAndBedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Year;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OlderService {
    @Autowired
    OlderMapper olderMapper;
    @Autowired
    BedMapper bedMapper;

    //查询所有老人信息
    public PageInfo<OlderVo> selectOlderByCons(int now, int size, String name, int status, int labelId, int careId) {
        PageHelper.startPage(now, size);
        List<OlderVo> olderVoList = olderMapper.selectOlderByCons(name, status, labelId, careId);
        PageInfo<OlderVo> pageInfo = new PageInfo<>(olderVoList);
        return pageInfo;
    }

    public Older selectOlderById(int id) {
        return olderMapper.selectByPrimaryKey(id);
    }

    public void updateConsulter(Older older) {
        olderMapper.updateByPrimaryKey(older);
    }

    @Transactional
    public void insertOlder(Older older) {
        olderMapper.insert(older);
        int bedId = older.getBedId();
        bedMapper.updateBedStateByNewId(bedId);
    }

    //确认退房
    @Transactional
    public void updateOlderStatusById(int id) {
        Older older = olderMapper.selectByPrimaryKey(id);
        int bid = older.getBedId();
        olderMapper.updateOlderStatusById(id);
        bedMapper.updateBedStateById(bid);
    }

    //老人登记退院
    public void updateOlderStatusById(int id, Date exitdate) {
        olderMapper.updateOlderStatusAndExitById(id, exitdate);
    }

    //查询status不为1 的 长者信息
    public PageInfo<Older> selectOlderByExitStatus(int now, int size, String name) {
        PageHelper.startPage(now, size);
        List<Older> olderVoList = olderMapper.selectOlderByStatus(name, 0);
        PageInfo<Older> pageInfo = new PageInfo<>(olderVoList);
        return pageInfo;
    }

    //查询status为1 的长者信息
    public PageInfo<Older> selectOlderByStatus(int now, int size, String name) {
        PageHelper.startPage(now, size);
        List<Older> olderVoList = olderMapper.selectOlderByStatus(name, 1);
        PageInfo<Older> pageInfo = new PageInfo<>(olderVoList);
        return pageInfo;
    }

    public List<Bed> selectAllBed() {
        return bedMapper.selectAll();
    }

    public OlderVo searchOlderByBid(int bedId) {
        return olderMapper.searchOlderByBid(bedId);
    }

    public List<OlderAndBedVo> addEleSelectOlder(int buildingId, int storeyId, int roomId) {
        return olderMapper.selectOlderAndByBuildingAndStoreyAndRoomId(buildingId, storeyId, roomId);
    }

    public List<NumVo> oldNumByOld() {
        List<NumVo> numVos = new ArrayList<>();
        NumVo numVo = new NumVo();
        numVo.setName("60岁以下");
        int i = olderMapper.selectNumByOld(0, 60);
        numVo.setValue(i);
        numVos.add(numVo);
        NumVo numVo2 = new NumVo();
        numVo2.setName("60~70");
        int i1 = olderMapper.selectNumByOld(60, 70);
        numVo2.setValue(i1);
        numVos.add(numVo2);
        NumVo numVo3 = new NumVo();
        numVo3.setName("70~80");
        int i2 = olderMapper.selectNumByOld(70, 80);
        numVo3.setValue(i2);
        numVos.add(numVo3);
        NumVo numVo4 = new NumVo();
        numVo4.setName("80~90");
        int i3 = olderMapper.selectNumByOld(80, 90);
        numVo4.setValue(i3);
        numVos.add(numVo4);
        NumVo numVo5 = new NumVo();
        numVo5.setName("90岁以上");
        int i4 = olderMapper.selectNumByOld(90, 150);
        numVo5.setValue(i4);
        numVos.add(numVo5);
        return numVos;
    }

    public List<Integer> olderCheck(Integer year) {
        List<Integer> count = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            Integer mon = olderMapper.selectOlderCheck(year, i);
            count.add(mon);
        }
        return count;
    }
}
